package com.tarena.lbs.content.controller;

import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 处理文章分类 业务模块功能
 */
@Tag(name = "文章分类管理")
@RestController
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;

    /**
     * 插入分类
     * ~
     * @return
     */
    @Operation(summary = "文章分类列表查询")
    @GetMapping("/admin/content/category/list")
    public Result<PageResult<ArticleCategoryVO>> pageList(ArticleCategoryQuery query){
        log.debug("文章分类列表查询,查询参数： {}",query);
        PageResult<ArticleCategoryVO> pageResult = categoryService.pageList(query);
        return new Result<>(pageResult);
    }

    @Operation(summary = "文章分类保存")
    @PostMapping("/admin/content/category/insert")
    public Result<Void> addCategory(@RequestBody ArticleCategoryVO param){
        log.debug("文章分类保存,参数： {}",param);
        categoryService.categoryAdd(param);
        return Result.success();
    }
    
    @Operation(summary = "文章分类修改")
    @PostMapping("/admin/content/category/update")
    public Result<Void> categoryEdit(@RequestBody ArticleCategoryVO param){
        log.debug("文章分类修改,参数： {}",param);
        categoryService.categoryUpdate(param);
        return Result.success();
    }
    
    @Operation(summary = "文章分类删除")
    @RequestMapping(value = "/admin/content/category/delete", method = {RequestMethod.DELETE, RequestMethod.POST})
    public Result<Void> categoryDel(@RequestParam(value = "id", required = false) Integer id, 
                                   @RequestBody(required = false) ArticleCategoryVO param){
        log.debug("文章分类删除,参数id： {}, param: {}", id, param);
        // 支持多种参数传递方式
        Integer categoryId = id;
        if (categoryId == null && param != null && param.getId() != null) {
            categoryId = param.getId();
        }
        
        if (categoryId == null) {
            throw new IllegalArgumentException("分类ID不能为空");
        }
        categoryService.categoryDel(categoryId);
        return Result.success();
    }
}